#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <stack>
#include <string>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    void createSt(TreeNode*& root, string& str)
    {
        if (root == nullptr)
            return;
        int size = str.size();
        int flag = 0;
        if (size == 0 || str[size - 1] == '(') str += to_string(root->val);
        else
        {
            str += '(';
            str += to_string(root->val);
            flag = 1;
        }
        if (root->left || root->right)
        {
            str += '(';
            createSt(root->left, str);
            str += ')';
        }
        createSt(root->right, str);
        if (flag == 1)
            str += ')';
    }
    string tree2str(TreeNode* root) {
        string str = "";
        createSt(root, str);
        return str;
    }
};  

int main()
{
	return 0;
}
